<template>
  <el-dialog
    title="新增签名"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :append-to-body="true"
    :modal-append-to-body="true"
    :visible="visible"
  >
    <div class="content">
      <vueEsign ref="signFile" :width="800" :bg-color="bgColor" />
    </div>
    <div slot="footer" style="text-align: center; margin-top: 20px">
      <el-button @click="clear">重 置</el-button>
      <el-button type="primary" @click="save">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import vueEsign from './vueEsign'
import * as fileApi from '@/api/base/file'
export default {
  components: {
    vueEsign
  },
  props: {
    fileKey: {
      type: String,
      default: null
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      fileUri: null,
      bgColor: '#fff'
    }
  },
  computed: {
    user() {
      return this.$store.getters.user
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    handleClose() {
      this.$emit('update:visible', false)
    },
    clear() {
      this.$refs.signFile.reset()
    },
    reset() {
      this.fileUri = fileApi.getfileUpUri(this.fileKey)
      this.$nextTick(() => {
        this.$refs.signFile.reset()
      })
    },
    async save() {
      const file = await this.$refs.signFile.generate()
      this.saveFile(file)
    },
    async saveFile(blob) {
      const that = this
      const res = await fileApi.upFileStream(this.fileUri, blob, 'sign.png', (num) => {
        that.$emit('progress', num)
      })
      this.$emit('update:visible', false)
      this.$emit('save', res)
    }
  }
}
</script>

<style scoped>
	.content {
		width: 100%;
		height: 380px;
    text-align: center;
	}
</style>
